/*******************************************************************************
 * Copyright (c) 2011 EclipseDevX.org and others.
 * 
 * All rights reserved. This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License v1.0 which
 * accompanies this distribution, and is available at:
 * 
 *     http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/

package org.eclipsedevx.core.errors.ext;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.Platform;

public class MarkersExtManager {

	public static MarkersExtManager getInstance() {
		if (INSTANCE == null) {
			INSTANCE = new MarkersExtManager();
		}

		return INSTANCE;
	}

	private static MarkersExtManager INSTANCE;

	private Map<String, String> idToLabelMap = new HashMap<String, String>();

	private MarkersExtManager() {
		readTypes();
	}

	public String getLabelForMarkerType(IMarker marker) {
		try {
			return getLabelforTypeId(marker.getType());
		} catch (CoreException ce) {
			return "";
		}
	}

	public String getLabelforTypeId(String typeId) {
		if (typeId == null) {
			return "";
		} else {
			return idToLabelMap.get(typeId);
		}
	}

	private void readTypes() {
		idToLabelMap.put(IMarker.PROBLEM, "Problem");
		idToLabelMap.put(IMarker.TASK, "Task");
		idToLabelMap.put("org.eclipse.jdt.core.problem", "Java Problem");

		IExtensionPoint point = Platform.getExtensionRegistry()
				.getExtensionPoint(ResourcesPlugin.PI_RESOURCES, ResourcesPlugin.PT_MARKERS);

		if (point != null) {
			// Gather all registered marker types.
			IExtension[] extensions = point.getExtensions();

			for (int i = 0; i < extensions.length; ++i) {
				IExtension ext = extensions[i];
				String id = ext.getUniqueIdentifier();
				String label = ext.getLabel();
				if (label.equals("")) {

				} else {
					idToLabelMap.put(id, label);
				}
			}
		}
	}

}
